#include "mydialog.h"
#include "ui_mydialog.h"
#include <QPropertyAnimation>
#include <QxtGlobalShortcut>
#include <qrect.h>
#include <QDebug>
#include <QString>
#include <QShortcut>

MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    ui->setupUi(this);
    this->loadQml("/home/emmanuel/CatHome/qml/home/main.qml");
    QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(this);
    connect(shortcut, SIGNAL(activated()), this, SLOT(toggleVisibility()));
    shortcut->setShortcut(QKeySequence("Ctrl+Shift+F12"));


    QShortcut* qShort = new QShortcut(QKeySequence(Qt::Key_F5), this);
    qShort->setContext(Qt::ApplicationShortcut);
    connect(qShort, SIGNAL(activated()), this, SLOT(refreshQml()) ) ;
    visible=true;

}

MyDialog::~MyDialog()
{
    delete ui;
    delete mAnimation;
    delete visibleRect;
}

void MyDialog::loadQml(QString sFileName)
{
    qmlFileName=sFileName;
    refreshQml();
}
void MyDialog::refreshQml()
{
    ui->declarativeView->setSource(QUrl::fromLocalFile(qmlFileName));
}

void MyDialog::toggleVisibility()
{

        mAnimation = new QPropertyAnimation(this, "geometry");
        mAnimation->setDuration(1000);
        mAnimation->setEasingCurve(QEasingCurve::InOutQuad);

        if(visible){
            this->visibleRect = new QRect(this->geometry());
            QRect invisibleRect = this->visibleRect->adjusted(0,0,0,0);
            invisibleRect.setRight(0);
            left = this->geometry().left();
            mAnimation->setEndValue(invisibleRect);
            visible=false;
        }else{
            mAnimation->setEndValue(*visibleRect);
            visible=true;
        }
        mAnimation->start();
}


